<template>
    <div class="user">
<!--        面包导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>

<!--        卡片-->
        <el-card class="box-card">
<!--            搜索区域-->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入账号或者姓名"  v-model="queryInfo.name" clearable>
<!--                        <el-select  slot="prepend" placeholder="请选择" >-->
<!--                            <el-option label="餐厅名" value="1"></el-option>-->
<!--                            <el-option label="订单号" value="2"></el-option>-->
<!--                            <el-option label="用户电话" value="3"></el-option>-->
<!--                        </el-select>-->
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="dialogVisible = true">添加</el-button>
                    <el-button type="primary" @click="dialogVisible = true">导入</el-button>
                </el-col>
            </el-row>
<!--            表格区域-->
                <el-table :data="userList" border>
                    <el-table-column type="index"></el-table-column>
                    <el-table-column label="姓名" prop="name"></el-table-column>
                    <el-table-column label="账号" prop="account"></el-table-column>
                    <el-table-column label="身份" prop="roalName"></el-table-column>
                    <el-table-column label="学院" prop="cname"></el-table-column>
                    <el-table-column label="班级" prop="classname"></el-table-column>
                    <el-table-column label="创建时间" prop="createDate"></el-table-column>
                    <el-table-column label="状态" >
                        <template >
                            <el-switch ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作"  width="180px">
                        <template slot-scope="scope">
<!--                            修改-->
                            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
<!--                            删除-->
                            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUser(scope.row.id)"></el-button>
<!--                            分配角色-->
                            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
                            <el-button type="warning" icon="el-icon-setting" size="mini" @click="insertSetRoals(scope.row)"></el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>
            <!--                分页-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.page"
                    :page-sizes="[5,10,20,50]"
                    :page-size="queryInfo.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>

<!--        添加框-->
        <el-dialog
                title="添加用户"
                :visible.sync="dialogVisible"
                width="50%" @close="addClose">
<!--            表单-->
            <el-form :model="addFoem" :rules="addFoemRules" ref="ruleFormRef" label-width="70px" >
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="addFoem.name"></el-input>
                </el-form-item>
                <el-form-item label="账号" prop="account">
                    <el-input v-model="addFoem.account"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addFoem.password"></el-input>
                </el-form-item>
<!--                <el-form-item label="学院" prop="password">-->
<!--                    <el-input v-model="addFoem.password"></el-input>-->
<!--                </el-form-item>       -->
<!--                <el-form-item label="班级" prop="password">-->
<!--                    <el-input v-model="addFoem.password"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="学院" prop="cname">
                    <el-select  placeholder="请选择学院" v-model="addFoem.cname" @change="getClass(addFoem.cname)">
                        <el-option v-for="item in collegeList" :key="item.id" :label="item.cname" :value="item.id" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="班级" prop="classname">
                    <el-select  placeholder="请选择班級" v-model="addFoem.classname" @click="pdCollege()" :disabled="isClassname">
                        <el-option v-for="item in classList" :key="item.id" :label="item.classname" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
<!--                角色-->
                    <p>角色：<el-select v-model="addFoem.roal" multiple placeholder="请选择">
                        <el-option
                                v-for="item in roalList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                    </p>
            </el-form>
<!--            按钮-->
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
        </el-dialog>
<!--        修改框-->
        <el-dialog
                title="修改信息"
                :visible.sync="editDialogVisible"
                width="50%" >
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px" >
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="editForm.name" ></el-input>
                </el-form-item>
                <el-form-item label="账号">
                    <el-input v-model="editForm.account" disabled></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password" ></el-input>
                </el-form-item>
                <el-form-item label="学院" >
                    <el-input v-model="editForm.cname" disabled></el-input>
                </el-form-item>
                <el-form-item label="班级" >
                    <el-input v-model="editForm.classname" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserInfo">确 定</el-button>
  </span>
        </el-dialog>

        <el-dialog
                title="分配角色"
                :visible.sync="setRoal"
                width="50%">
         <div>
             <p>当前用户：{{infos.name}}</p>
             <p>当前用户角色：{{infos.roalName}}</p>
             <!--                角色-->
            <p>新角色： <el-select v-model="roalAdd" multiple placeholder="请选择"  prop="roal">
                 <el-option
                         v-for="item in roalList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
                 </el-option>
             </el-select>
            </p>
         </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="setRoal = false">取 消</el-button>
    <el-button type="primary" @click="updateRoal">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "user",
        data(){
            //
            // var chekcEmil = (rule,value,cb) => {
            //     const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/   //邮箱
            //     // const regMobile =/^(0|86|17951)?(13[0-9]{8})$/  //手机号
            //     if(regEmail.test(value)){
            //         return cb();
            //     }
            //     cb(new Error('请输入合法的邮箱'))
            // }
            return{
                queryInfo:{
                    name: '',
                    page: 1,
                    size: 10
                },
                userList:[],
                total: 0,
                //控制添加框
                dialogVisible: false,
                //控制修改框
                editDialogVisible: false,
                //修改角色
                roalAdd: [],
                //表单数据对象
                addFoem: {
                    name: '',
                    account: '',
                    password: '',
                    classname: '',
                    cname: '',
                    roal: [],
                    token: ''
                },
                //查询到的用户数据
                editForm: {},
                //表单验证规则
                addFoemRules:{
                    name:[
                        { required: true,message: '请输入用户名',trigger: 'blur'},
                        { min: 1, max: 10,message: '请输入1到10之间',trigger: 'blur'}
                    ],
                    account:[
                        { required: true,message: '请输入账号',trigger: 'blur'},
                        { min: 9, max: 9,message: '只能输入九位',trigger: 'blur'}
                    ],
                    password:[
                        { required: true,message: '请输入密码',trigger: 'blur'},
                        { min: 6, max: 10,message: '只能输6-10',trigger: 'blur'},
                        // { validator: chekcEmil,trigger: 'blur'}
                    ],
                    classname:[
                        { required: true,message: '请选择班级',trigger: 'change'},

                    ],
                    cname:[
                        { required: true,message: '请选择学院',trigger: 'change'}
                    ]
                },
                editFormRules:{
                    name:[
                        { required: true,message: '请输入用户名',trigger: 'blur'},
                        { min: 1, max: 10,message: '请输入1到10之间',trigger: 'blur'}
                    ],
                    password:[
                        { required: true,message: '请输入密码',trigger: 'blur'},
                        { min: 6, max: 10,message: '只能输6-10',trigger: 'blur'},
                        // { validator: chekcEmil,trigger: 'blur'}
                    ]
                },
                //班级
                classList:{},
                //学院
                collegeList:{},
                //角色
                roalList:{},
                isClassname: true,
                //分配角色的显示与隐藏
                setRoal: false,
                //用户信息
                infos: {}
            }
        },
        created() {
            this.getUserList()
        },
        methods:{
            //获取用户列表
            async getUserList(){
                const {data: res} =await this.$http.get('User/getAllUserList',{
                    params: this.queryInfo
                })
                if(res.code != 200) return this.$message.error('数据获取失败')
                this.userList = res.data.data
                this.total = res.data.total
                //获取学院
                const {data: rey} =  await this.$http.get('Colledg/getAllCollege')
                this.collegeList = rey.data
                //获取角色
                const  {data: ret} = await this.$http.get('Roal/getAllRoalList',{
                    params:{
                        code: 3,
                        page: 1,
                        size: 100
                    }
                })
                this.roalList = ret.data.data
            },
            //判断院系是否判断
            pdCollege(){
                if (this.addFoem.cname == ''){
                    this.$message.info('请先选择院系')
                }
            },
            //获取班级列表
            async getClass(id){
                //获取班级
                const {data: ref} =  await this.$http.get('Colledg/getAllClass',{
                    params:{
                        id: id
                    }
                })
                this.classList = ref.data
                this.isClassname = false
            },
            //监听pagesize的改变
            handleSizeChange(newSize){
                this.queryInfo.size = newSize
                this.getUserList()
            },
            //页码值改变
            handleCurrentChange(newPage){
                this.queryInfo.page = newPage
                this.getUserList()
            },
            //关闭表单并重置
            addClose(){
                this.classList = ''
                this.addFoem.roal = []
                this.isClassname = true
                this.$refs.ruleFormRef.resetFields()
            },
            //添加用户
             addUser(){
                this.$refs.ruleFormRef.validate(async valid => {
                    if(!valid) return
                    //添加用户
                    this.addFoem.token = window.sessionStorage.getItem('token')
                    const { data: res} = await this.$http.post('User/insertUser',this.addFoem)
                    if(res.code !== 0) {
                        return this.$message.error(res.message)
                    }else {
                        this.$message.success('添加成功');
                    }

                    //关闭窗口
                    this.dialogVisible = false;
                    //刷新
                    this.getUserList();
                })
            },
            //展示编辑框
            async showEditDialog(id){
                const {data: res} =await this.$http.get('User/getUserByid',{
                    params:{
                        id: id,
                    }
                })
                if(res.code != 200) return this.$message.error('查询数据失败')
                this.editForm = res.data
                this.editDialogVisible = true;
            },
            //修改用户信息
            editUserInfo(){
                this.$refs.editFormRef.validate(async valid => {
                    if(!valid) return
                    //修改用户信息
                    const {data: res} = await this.$http.post('User/updateUser',this.editForm)
                    if(res.code !== 0) return this.$message.error(res.message)
                    this.$message.success(res.message)

                    //关闭修改框
                    this.editDialogVisible = false
                    this.getUserList()
                })
            },
            //删除用户
            async removeUser(id){
                const confirm = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err =>{
                    return err
                })
                if(confirm !== 'confirm') return this.$message.info('已取消删除')

                const {data:res} = await this.$http.get('User/deleteUser',{
                    params:{
                        id: id
                    }
                })
                this.$message.success(res.message)
                this.getUserList()
            },
            //展示分配框
            insertSetRoals(userInfo){
                this.infos = userInfo
                this.setRoal = true
            },
            //修改角色
            async updateRoal(){
                const {data: res} = await this.$http.post('UserRoal/insertUserRoal',{
                    rids: this.roalAdd,
                    uid: this.infos.id
                })
                if(res.code !==0){
                    return this.$message.error(res.message)
                }
                this.roalAdd = []
                this.getUserList()
                this.setRoal = false
            }

        }
    }
</script>

<style lang="less" scoped>
    .box-card{
        box-shadow: 0 1px 1px rgba(0,0,0,0.15) !important;
    }
    .el-table{
        margin-top: 15px;
        font-size: 12px;
    }
</style>